Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't this local + server stun script work?

Asked by 4 years ago

So I have a script in StarterPack, that checks when a stun value is true, and then when the value is true, it uses a remote event to do my stun function, however, when the stun value is true, the script never activates and i'm not sure why.

Local Script:

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local root = Character:WaitForChild("HumanoidRootPart")
local stunned = Character:WaitForChild("Stunned")
local stuntime = 1


repeat wait() until stunned
if stunned.Value == true then
        script.RemoteEvent:FireServer(Player, stunned, root)
    end

Remote event:

local Cooldown = false
local stuntime = 1

script.Parent.OnServerEvent:Connect(function(Player, stunned, root)
    root.Anchored = true
    wait(stuntime)
    root.Anchored = false
    stunned.Value = false
end)

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Your localscript is saying "wait until the stunned value exists" when it does exist, it checks if the stunned value is true, then if it is it fires the server.

Instead, we want to use the .Changed event to run a funciton every time the stunned value changes:

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local root = Character:WaitForChild("HumanoidRootPart")
local stunned = Character:WaitForChild("Stunned")
local stuntime = 1

stunned.Changed:Connect(function()
    if stunned.Value == true then
        script.RemoteEvent:FireServer(Player)
    end
end

You also don't need to pass the stunned value/the humanoid root part through the remote event. Just get it from the character:

local Cooldown = false
local stuntime = 1

script.Parent.OnServerEvent:Connect(function(Player)
    local character = player.Character
    assert(character, "character doesnt exist") --error if the character doesnt exist
    local stunned = player.Character.Stunned

    character.HumanoidRootPart.Anchored = true
    wait(stuntime)
    character.HumanoidRootPart.Anchored = false
    stunned.Value = false
end)

Troubleshoot:

  • Check for any errors in the output, if there are any errors, send them here.
  • See if the localscript is running, print("localscript is running") at the top of the localscript
  • See if the server script is running: print("script is running") at the top of the script
  • confirm that the remote is the script.Parent in the server script
  • make sure that the event is running by printing the player inside of the onserverevent: print(Player)
1
let me know if you still have problems after trying the new code / following the advice in troubleshoot list. it's ok if u do. I just want to make sure you get your code working. royaltoe 5144 — 4y
0
Thanks a lot for the help. I was able to get the scripts working up until the last line of the server script, the "stunned.Value = false", the character gets rooted, but the stun value never turns off. bowsercow 20 — 4y
0
Ah, that's because we never defined stunned. My bad. I will do that right now. royaltoe 5144 — 4y
0
see my edit please royaltoe 5144 — 4y
View all comments (2 more)
0
Thank you so much, the script works. bowsercow 20 — 4y
0
Cheers royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Pretty common problem but it's because scripts run up and down once.

Summary from your local script:

local a=1;

if a==1 then--since a==1 then the if statement code is ran and the print function is printed, however notice how it only prints once. this means the script doesnt go back to that if statement more than once unless, you know, loops or functions. therefore if a~=1 then it would be impossible to print 'wow potato' from the line below
    print'wow potato';
end;

I suggest an event on the value, like .Changed so that the value can change anytime and the if statement can be repeatedly ran.

workspace.Changed:Connect(function()
    print(workspace.Name); 
end;
wait(1);
workspace.Name='wow potato';-->event was fired "wow potato"
wait(1);
workspace.Name='wow potato 2';-->event was fired "wow potato 2"

Answer this question