View Single Post
Tank_in_Pink
Member
Join Date: Feb 2016
Old 03-13-2018 , 04:17   Re: What am I doing wrong?
Reply With Quote #3

Thanks to all of you guys, especially thanks for this:

Quote:
Originally Posted by 1337norway View Post
you can't add variables to a dynamic string like this in sourcemod:
PHP Code:
"You are dead. You will be respawned at a random location in" countdownTimer "seconds." 
you have a string that parses variables based off of tokens like this:
PHP Code:
PrintCenterText(client"You are dead. You will be respawned at a random location in %i seconds."countdownTimer
Also you are creating a repeating timer each player death event and never killing it. So you have infinite looping timers with a big memory leak, even worse then a memory leak you have code that's being run and useless. You're also using a variable that all clients would share instead of a variable per client. The variable you are defining is only accessible within that scope of code and not functions called under it. you have to define that variable outside of all scopes to be global for all functions to access. You're also trying to parse the event inside a child function which you can't do either in sp like this without passing the event itself.

Here's a fixed up version of your code to help you learn for your specific case.
PHP Code:
#include <sourcemod>

public Plugin:myinfo 
{
    
name "...",
    
author "...",
    
description "...",
    
version "1.0",
    
url "..."
}

int countdownTimer[MAXPLAYERS+1] = {30, ...};

public 
OnPluginStart()
{
    
HookEvent("player_death"Event_PlayerDeath);
}

public 
Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event,"userid"));
    if(!
IsValidClient(client))
        return;
        
    
countdownTimer[client] = 30;
    
CreateTimer(1.0PrintMessageGetClientUserId(client), TIMER_REPEAT);
}

public 
Action PrintMessage(Handle timer)
{
    new 
client GetClientOfUserId(clientid);
    if(!
IsValidClient(client))
        return 
Plugin_Stop
    
    
countdownTimer[client]--;

    if(
countdownTimer[client] == 0)
        return 
Plugin_Stop;
        
    
PrintCenterText(client"You are dead. You will be respawned at a random location in %i seconds."countdownTimer[client]);
 
    return 
Plugin_Continue;
}

bool IsValidClient(int client)
{
    if (!(
<= client <= MaxClients) || !IsClientConnected(client) || !IsClientInGame(client) || IsClientSourceTV(client) || IsClientReplay(client))
        return 
false;
        
    return 
true;

Also search here how to correctly use functions:
https://sm.alliedmods.net/new-api/
I still have two questions about your code would be awesome if you could explain.

Why ''MAXPLAYRRS+1'' / what does is affect?

What does ''IsValidClient'' mean?


Thanks for your help already,
hazetank
Tank_in_Pink is offline