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

Scripts not working on a server. Only works on test mode?

Asked by
MrHerkes 166
7 years ago
Edited 7 years ago

Hello! You must've heard me from my previous question about the landmine script problem (or not). Well I took advice from blowup999 (the person that helped me with my landmine script). It looked like my game was done, until when I created a server, some scripts were not working: my landmine script and my Press R to reset script. They were working normally on ROBLOX Studio test but not on an actual server. Note: Both scripts are normal, not localscript. LANDMINE SCRIPT:

canExplode = true;
function ExplodePart(part)
    if canExplode then
        canExplode = false
        game.Players.LocalPlayer.Character.Humanoid.Health = 0
        local explosion = Instance.new("Explosion")
        explosion.BlastRadius = 50
        explosion.Position = part.Position
        explosion.Parent = game.Workspace
        wait(1)
        canExplode = true
    end
end

landMine = game.Workspace.Landmine

landMine.Touched:connect(ExplodePart)

PRESS R TO RESET SCRIPT:

function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        game.Players.LocalPlayer.Character.Humanoid.Health = 0
        print("Dead")
    end
end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R)

Once again, please help a beginner scripter! :)

1 answer

Log in to vote
2
Answered by 7 years ago

Several things are wrong here. For one, you're attempting to essentially get input from the server. As the server isn't a player, and has no inputs to be bound to (Thru ContextActionService, or UserInputService) so your scripts are not working. You're also using direct references(EX: workspace.part.whatever) which isn't recommended, and will eventually cause you even more problems. For future reference, make things relative whenever you can (EX:script.Parent, which would act on the script you run it from's parent, so it doesn't matter if the part's hierarchy changes).

The code reworked:

Landmine:

canExplode = true;
function ExplodePart(part)
    if canExplode and part.Parent:FindFirstChild("Humanoid") then
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
        canExplode = false
        player.Character.Humanoid.Health = 0
        local explosion = Instance.new("Explosion")
        explosion.BlastRadius = 50
        explosion.Position = part.Position
        explosion.Parent = game.Workspace
        wait(1)
        canExplode = true
    end
end

landMine = script.Parent --Use this if script is child of your landmine, otherwise replace with direct reference(workspace.part.blabla)

landMine.Touched:connect(ExplodePart)

Explanation: All I've done here is replaced one of your direct references, with a system that determines if it was a player that touched the part. It goes to the part's parent, and then checks if there is a player that owns that model. If there is, it gets the player object using GetPlayerFromCharacter, and then applies the damage to that. If there is no player attached to that model, the second argument of the if statement is nil, so none of the code is run and no harm done.

Second Script:

This one is all correct, you just have it in the wrong script type and place. What you need to do is put the code into a localScript, which you then manually insert into StarterPlayerScripts (accessed thru game>StarterPlayer>StarterPlayerScripts)

Hope this helped!

0
I understand now, thanks for the helpful info! ^_^ MrHerkes 166 — 7y
Ad

Answer this question